package net.lahuobao.mall.service.impl.admin;

import net.lahuobao.mall.entity.admin.MallConfigEtcEntity;
import net.lahuobao.mall.entity.admin.MallEtcApplyEntity;
import net.lahuobao.mall.entity.admin.MallEtcCardRuleEntity;
import net.lahuobao.mall.entity.admin.MallEtcUserEntity;
import net.lahuobao.mall.service.admin.MallEtcApplyServiceI;
import net.lahuobao.mall.service.admin.MallEtcCardRuleServiceI;
import net.lahuobao.mall.service.admin.MallEtcUserServiceI;
import net.lahuobao.util.httpClientUtil;
import net.sf.json.JSONObject;
import org.apache.http.message.BasicNameValuePair;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

@Service("mallEtcApplyService")
@Transactional
public class MallEtcApplyServiceImpl extends CommonServiceImpl implements MallEtcApplyServiceI {

    @Autowired
    private MallEtcUserServiceI etcUserService;

    @Autowired
    private MallEtcCardRuleServiceI etcCardRuleService;

    @Override
    public void configEtcCardNo(MallConfigEtcEntity configEtcEntity) throws Exception{
        //获取商城url地址
        ResourceBundle bundle = PropertyResourceBundle.getBundle("shopping");
        String url = bundle.getString("lhbmall") + bundle.getString("etccardconfig");

        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(new BasicNameValuePair("orderId", String.valueOf(configEtcEntity.getOrderId())));
        nvps.add(new BasicNameValuePair("receiveType", String.valueOf(configEtcEntity.getReceiveType())));
        nvps.add(new BasicNameValuePair("expresscompanyId", String.valueOf(configEtcEntity.getExpresscompanyId())));
        nvps.add(new BasicNameValuePair("expresscompanyNo", String.valueOf(configEtcEntity.getExpresscompanyNo())));

        JSONObject json = httpClientUtil.httpPost(url, nvps, false);

        if(json != null && (Boolean)json.get("handleState")){
            MallEtcApplyEntity t = super.get(MallEtcApplyEntity.class, configEtcEntity.getId());
            MallEtcApplyEntity update = new MallEtcApplyEntity();
            //状态设为--待收货
            update.setState(3);
            update.setEctcardno(configEtcEntity.getEtcCardNo());
            //更新ETC申请表
            MyBeanUtils.copyBeanNotNull2Bean(update, t);
            super.saveOrUpdate(t);

            //更新ETC规则表 状态
            MallEtcCardRuleEntity rulet = etcCardRuleService.get(MallEtcCardRuleEntity.class, configEtcEntity.getEtcCardRuleId());
            MallEtcApplyEntity ruletUpdate = new MallEtcApplyEntity();
            //已经使用
            ruletUpdate.setState(2);
            MyBeanUtils.copyBeanNotNull2Bean(ruletUpdate, rulet);
            etcCardRuleService.saveOrUpdate(rulet);

            //写入ETC用户关系表
            MallEtcUserEntity etcUserEntity = new MallEtcUserEntity();
            //状态为激活
            etcUserEntity.setState(1);
            etcUserEntity.setCardno(configEtcEntity.getEtcCardNo());
            etcUserEntity.setCarno(t.getCarno());
            etcUserEntity.setUserid(t.getApplyuserid());
            etcUserService.save(etcUserEntity);
        }else{
            throw new RuntimeException(null == json ? "系统异常！" : json.get("message").toString());
        }
    }

}